/************************************************************************
*************************************************************************
*************************************************************************

prodestcon_colombia.do

Runs appendix tests in the Colombian manufacturing data used by Gandhi et al.


*************************************************************************
*************************************************************************
************************************************************************/



//Load sharetest
do "do/programs/sharetest.do"

/*****************************************************************
******************************************************************
Helper programs
******************************************************************
*****************************************************************/

/*****************************************************************
Product
*****************************************************************/

//Recursive program to get the pairwise products of a set of terms
cap program drop product
program product
	syntax varlist
	
	gettoken firstvar resvars : varlist	
		
	//Take the product
	foreach var of varlist `varlist' {
		gen `firstvar'X`var' = `firstvar'*`var'
	}
	
	//If we're not on the last word, keep going
	if !mi(word("`resvars'",1)) {
		//Continue recursion
		product `resvars'
	}
end


/*****************************************************************
formdatacol
*****************************************************************/
/*
This program simply applies the code given to me by Salvador Navarro
to form the Colombian data.

The only argument is the sector we want to keep raw materials in inventory
*/


cap program drop formdatacol
program formdatacol
		args ind
		

		keep if sic3==`ind'

		gen wages=wl/l
		bys year: egen mm=median(wages) if wages~=.
		gen hiwag=.
		replace hiwag=0 if wages<=mm & wages~=.
		replace hiwag=1 if wages>mm & wages~=.
		drop mm


		/*
		bys year: egen mm=median(ratiowcbc) 
		gen wcbc=.
		replace wcbc=0 if ratiowcbc<=mm 
		replace wcbc=1 if ratiowcbc>mm 
		drop mm ratiowcbc
		*/

		ren d_exp export
		gen import=0
		replace import=1 if import_inp==1 | import_mat==1
		replace import=. if (import_inp==. | import_mat==.) & import==0
		gen K=exp(logrk)
		drop logrk
		gen logrk=log(K)
		drop K

		keep plant year logrgo rgo Irgo logsii Isii logrva rva Irva logl l Il logrk rk Irk logrii rii Irii export import savk savk2 //adv hiwag wcbc 
		ren plant id
		ren logrgo logRGO
		ren rgo RGO 
		ren Irgo IRGO
		ren logsii si
		ren Isii Isi
		ren logrva logRVA
		ren rva RVA 
		ren Irva IRVA
		ren logl logL
		ren l L 
		ren Il IL
		ren logrk logK
		ren rk K
		ren Irk IK
		ren logrii logRI
		ren rii RI
		ren Irii IRI
		bys year: egen mm=median(logK) 
		gen sizek=.
		replace sizek=0 if logK<mm & logK~=.
		replace sizek=1 if logK>=mm & logK~=.
		drop mm

		scalar scale = 10
		replace logRGO = logRGO - ln(scale)
		replace RGO = RGO / scale
		replace logRVA = logRVA - ln(scale)
		replace RVA = RVA / scale
		replace logK = logK - ln(scale)
		replace K = K / scale
		replace logRI = logRI - ln(scale)
		replace RI = RI / scale


		#delimit;
		order logRGO IRGO si Isi logRI IRI logK IK id year 
			logL IL export import sizek   ;  //adv hiwag wcbc
		#delimit cr
		sort id year
		compress
		
		//This command is my own; I don't know why log(0) was set to -1000, but I undo it here;
		//since this has to be done to match GNR's OLS results, I'm pretty sure they do the same thing
		recode logL logK logRI logRGO (-1000 = .)

end



/*****************************************************************
******************************************************************
Run Tests
******************************************************************
*****************************************************************/

local industrylist 311 321 322 331 381

//Want to control for year dummies?
local tcon i.t



/*****************************************************************
Prepare Colombia data for analysis
*****************************************************************/

/***********
The following code is taken from the files sent by Salvador Navarro;
The only change is to keep n1 (inventory of raw materials at the 
beginning of the year).
***********/ 


/*   ----------------Begin Outside Code---------------------      */
use "dta/mergecap.dta", clear

rename n5 savk
rename x3 savk2


xtset plant datayear


foreach var of varlist p_machin p_struc p_transp p_gdp {
	egen mep= mean(`var'), by (datayear)
	gen tt= mep if datayear==81
	egen ep_81= max(tt)
	gen epindex= `var'/ep_81
	rename `var' `var'_old
	rename epindex `var'
	drop tt ep_81 mep 
}	

gen import_inp=.
replace import_inp=0 if s7==0
replace import_inp=1 if s7>0 & s7!=.
gen import_mat=.
replace import_mat=0 if s11==0
replace import_mat=1 if s11>0 & s11!=.

gen adv=.
replace adv=0 if c13==0 & c13~=.
replace adv=1 if c13>0 & c13~=.

replace rland_80=rland_80*p_struc_old/p_struc
replace rbldg_80=rbldg_80*p_struc_old/p_struc
replace rmach_80=rmach_80*p_machin_old/p_machin
replace rtrans_80=rtrans_80*p_transp_old/p_transp
replace roffice_80=roffice_80*p_machin_old/p_machin
local i=80
replace rcap`i'=rland_`i'+ rbldg_`i'+ rmach_`i'+ rtrans_`i'+ roffice_`i'
replace rcap`i'=. if rcap`i'<= 0

/*** PRICE INDEX FOR ENERGY INPUTS ***/

/*** USING QUANTITY AND VALUE OF ENERGY CONSUMED ***/
/*** AVAILABLE FROM 1981 ***/

gen epr= e7/ e4
egen mepr= mean(epr), by (datayear)
gen tt= mepr if datayear==81
egen epr_81= max(tt)
drop tt
gen eprindex= epr/epr_81
label var eprindex "price index of energy (consumed, base 1981)"

/*** TOTAL ENERGY INPUT (REAL VALUE) ***/

gen renergy= e7/eprindex 
label var renergy "REAL ENERGY VALUE CONSUMED AT 1981 VALUE"

drop if datayear<81

gen split = s3/s5
replace split = . if split>1
gen l_split = ln(split)

gen go = pg
label var go "nominal gross ouput"
gen gd = split * pg
label var gd "nominal gross domestic ouput"
gen ge = (1-split) * pg
label var ge "nominal gross exports ouput"

*replace p_output = pg / rp if p_output == .
*gen rgo = go / p_output
*gen rgd = gd / p_output
*gen rge = ge / p_output

gen rgo = go / p_gdp
label var rgo "real gross ouput, base 1981"
gen rgd = gd / p_gdp
label var rgd "real gross domestic ouput, base 1981"
gen rge = ge / p_gdp
label var rge "real gross exports ouput, base 1981"

gen rk = rcap80
replace rk=. if rk<=0
label var rk "real capital stock, base 1981"
gen rinv= (inv_land/p_struc) + (inv_bldg/p_struc) + (inv_mach/p_machin) + (inv_trans/p_transp) + (inv_office/p_machin)
replace rinv=. if rinv<=0
label var rinv "real investment, base 1981"

gen l = sklab*(skwages/sklab)/(unskwages/unsklab) + unsklab
label var l "labor (employees)"
gen yl = l*x7/12 
drop l
rename yl l
label var l "labor (employee years)"

gen ratiowcbc = sklab/unsklab
label var ratiowcbc "ratio wc bc workers"

gen wl = skwages + unskwages
label var wl "nominal wage bill"
gen rwl = wl / p_gdp
label var rwl "real wage bill, base 1981"
ren s10 mats
label var mats "nominal materials"
rename servpur serv
label var serv "nominal services"
rename e7 energy
label var energy "nominal energy consumed"
gen ii = mats + serv + energy
label var ii "nominal intermdiate inputs (mats+serv+energ)"
replace rmatused=rmatused*p_gdp_old/p_gdp
rename rmatused rmats
label var rmats "real materials, base 1981"
replace rserv=rserv*p_gdp_old/p_gdp
label var rserv "real services, base 1981"
gen rii = rmats + rserv + renergy
label var rii "real intermdiate inputs, base 1981"
gen iic = ic
label var iic "intermediate consumption"
gen riic = ic/p_gdp
label var riic "real intermediate consumption, base 1981"

gen ms = mats + serv
label var ms "nominal mats+serv"
gen rms = rmats + rserv
label var rms "real mats+serv, base 1981"

gen sii=ii/go
label var sii "share int inputs"
gen sn=energy/go
label var sn "share energy"
gen sl=wl/go
label var sl "share labor"

drop val* rva va*

gen va=go-ii
replace va=. if va<=0
label var va "nominal value added"
gen rva=rgo-rii
replace rva=. if rva<=0
label var rva "real value added, base 1981"

gen vams=go-ms
replace vams=. if vams<=0
label var va "nominal value added mats+serv"
gen rvams=rgo-rms
replace rvams=. if rvams<=0
label var rvams "real value added mate+serv, base 1981"

gen sn_vams=energy/vams
label var sn_vams "share energy vams" 
gen sl_va=wl/vams
label var sl_va "share labor va"

rename datayear year

gen d_exp = s4> 0 & s4~= .

gen switchflag = 0
sort plant year
bys plant: replace switchflag = 4 if sic[_n] ~= sic[_n-1] & sic[_n-1] ~= .
sort plant year
bys plant: replace switchflag = 3 if sic3[_n] ~= sic3[_n-1] & sic3[_n-1] ~= .
label var switchflag "Flag for Sector Switch in History of Establishment"

#delimit;
keep plant year go gd ge rgo rgd rge p_gdp va rva vams rvams sii sn sl sn_vams sl_va l wl rwl mats rmats savk savk2
	energy renergy serv rserv rk ii rii ms rms d_exp sic3 rinv switchflag import_inp import_mat adv ratiowcbc;
order plant year go rgo gd rgd ge rge p_gdp va rva vams rvams sii sn sl sn_vams sl_va l wl rwl mats rmats 
	energy renergy serv rserv rk ii rii ms rms d_exp sic3 rinv switchflag import_inp import_mat adv ratiowcbc;

reshape wide go rgo gd rgd ge rge va rva vams rvams sii sn sl sn_vams sl_va l wl rwl mats rmats savk savk2
	energy renergy serv rserv rk ii rii ms rms d_exp p_gdp rinv switchflag import_inp import_mat adv ratiowcbc, i(plant sic3) j(year);
reshape long go rgo gd rgd ge rge va rva vams rvams sii sn sl sn_vams sl_va l wl rwl mats rmats savk savk2
	energy renergy serv rserv rk ii rii ms rms d_exp p_gdp rinv switchflag import_inp import_mat adv ratiowcbc, i(plant sic3) j(year);
#delimit cr
bys year: egen a=mean(p_gdp)
replace p_gdp=a if p_gdp==.
drop a

foreach var of varlist go-rms rinv sic3 {
	replace `var'=. if `var'==0
	gen log`var'=ln(`var')
	gen I`var'=1
	replace I`var'=0 if log`var'==.
}

gen Id_exp=1
replace Id_exp=0 if d_exp==.
gen Iimport_inp=1
replace Iimport_inp=0 if import_inp==.
gen Iimport_mat=1
replace Iimport_mat=0 if import_mat==.

drop logp_gdp Ip_gdp logsic3 Isic3

#delimit;
order plant year go loggo Igo rgo logrgo Irgo gd loggd Igd rgd logrgd Irgd ge logge Ige rge logrge Irge d_exp Id_exp 
	p_gdp va logva Iva rva logrva Irva vams logvams Ivams rvams logrvams Irvams sii logsii Isii sn logsn Isn 
	sl logsl Isl sn_vams logsn_vams Isn_vams sl_va logsl_va Isl_va l logl Il wl logwl Iwl rwl logrwl Irwl 
	mats logmats Imats rmats logrmats Irmats energy logenergy Ienergy renergy logrenergy Irenergy serv logserv Iserv
	rserv logrserv Irserv rk logrk Irk ii logii Iii rii logrii Irii ms logms Ims rms logrms Irms rinv logrinv Irinv 
	sic3 import_inp import_mat adv ratiowcbc;
#delimit cr	 

compress
sort plant year

/*   ----------------End Outside Code---------------------      */





local count = 0
foreach ind of numlist 311 321 322 331 381 {
	local count = `count' + 1

	di "On industry `ind'..."
	
	quietly {
		preserve
		/*****************************************************************
		Make industry data
		*****************************************************************/
		
		formdatacol `ind'
		
		
		//Make variables with friendlier names
		gen t = year
		xtset id t
		
		gen y = logRGO
		gen k = logK
		gen l = logL
		gen m = logRI
		
		//Get higher-order terms
		product k l m
		
		sort id t

		gen logSHARE = si if si != -1000
		
		//Test for different degrees
		forvalues d=2/8 {
			sharetest logSHARE k l m, test( l2.( c.k##c.m ) ) lagtest deg(`d') lagdeg(1) con(i.t)
			
			foreach stat in N N_clust F pval r2 {
				gen shtest_`stat'_d`d' = r(`stat')
			}
		}
		
		gen ind = `ind'
		collapse ind shtest_* 
		
		tempfile `ind'
		save ``ind''
		
			
		restore
	}
		
	

}


//use `results', clear


clear
foreach ind in `industrylist' {
	if _N == 0 {
		use ``ind''
	}
	else {
		append using ``ind''
	}
}

order ind


cap mkdir "dta/emp_results"
save "dta/emp_results/colombia.dta", replace
